package springMvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sc")
public class StudentController extends BaseController {

    private List<Student> list = new ArrayList<Student>();

    @RequestMapping("/add")
    public String add(){
        return prefix + "/addStudent.jsp";
    }

    @RequestMapping("/getStudent")
    public String getStudent(Student stu, HttpServletRequest req){
        System.out.println("添加一位学生");
        stu.setAge(getAge(stu.getBirthday()));
        list.add(stu);

        req.setAttribute("ls",list);
        return prefix + "/showStudent.jsp";
    }

    public int getAge(String birthday){
        int age = 0;
        try {
            Calendar c = Calendar.getInstance();
            int nowYear = c.get(Calendar.YEAR);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(birthday);
            c.setTime(d);
            int birYear = c.get(1);
            age = Math.abs(birYear - nowYear);

        }catch (Exception e){
            e.printStackTrace();
        }
        return age;
    }

}
